package com.lagou.app;

import com.lagou.entity.Account;
import com.lagou.entity.Transaction;
import com.lagou.utils.DateUtils;
import com.lagou.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 第一题测试类
 * 完成账户的交易
 */
public class TestTra {

//    实现卡号：1122334455向55443332211转账5000元的操作；
    public static void main(String[] args) throws SQLException {

        //获取QueryRunner对象
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());

        //SQL语句
        String querysql = "select * from account where card = ?";
        //查询账号为1122334455的账户信息
        Account accountOut = queryRunner.query(querysql, new BeanHandler<Account>(Account.class), "1122334455");
        //判断该账户的余额是否大于5000，如果小于50000则退出程序
        if (accountOut.getBalance() < 5000) {
            System.out.println("账户余额不足！");
            System.out.println("账户余额为： " + accountOut.getBalance());
            System.exit(0);
        }
        //查询账号为55443332211的账户信息
        Account accountIn = queryRunner.query(querysql, new BeanHandler<Account>(Account.class), "55443332211");
        //如果账户不存在则推出
        if (accountIn == null) {
            System.out.println("该账户不存在！");
            System.exit(0);
        }
        //根据账号更改余额信息
        String updatesql = "update account set balance = ? where card = ?";
        //将记录插入transaction表中
        String insertsql = "insert into transaction values(?,?,?,?,?)";

        //1122334455账户转出5000元
        int updateOut = queryRunner.update(updatesql, accountOut.getBalance() - 5000, "1122334455");
        Object[] paramOut = {0, accountOut.getCard(), "转出", 5000, DateUtils.getDateFormart()};
        //55443332211账户转入5000元
        int updateIn = queryRunner.update(updatesql, accountIn.getBalance() + 5000, "55443332211");
        Object[] paramIn = {0, accountIn.getCard(), "转入", 5000, DateUtils.getDateFormart()};
        //若修改数据成功，则将转账记录数据插入transaction表中
        if (updateOut != 0 && updateIn != 0) {
            queryRunner.update(insertsql, paramOut);
            queryRunner.update(insertsql, paramIn);
        } else {
            System.out.println("转账失败！！");
            queryRunner.update(updatesql, accountOut.getBalance(), "1122334455");
            queryRunner.update(updatesql, accountIn.getBalance(), "55443332211");
            System.exit(0);
        }
        //得到account表中的信息
        List<Account> accountList = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));
        for (Account account : accountList) {
            System.out.println(account);
        }

        System.out.println("----------------------------------------------------------");
        //得到transaction表中的信息
        List<Transaction> transactionList = queryRunner.query("select * from transaction", new BeanListHandler<Transaction>(Transaction.class));
        for (Transaction transaction : transactionList) {
            System.out.println(transaction);
        }
        System.out.println("转账成功！！");

    }

}
